<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Admin;


class Cost extends Admin
{
    /**
     * 查询并渲染 成本计划总览页面
     */
    public function index()
    {
       $data = db('cost')
            ->field('project_name')
            ->distinct(true)
            ->select();
       // var_dump($data);die;
       return view('admin@cost/index',[
            'data'=>$data
       ]);
    }

    /**
     * [details 查询详情并渲染详情页面]
     * @return [type] [description]
     */
    public function details()
    {
        //接收数据
        $project_name = input('param.');
        // 取键名
        $key = key($project_name);
        // 数据准备
        $arr_plan = ['work_cost_plan','material_cost_plan','machine_cost_plan','safe_cost_plan','labour_cost_plan','else_cost_plan','charges_plan','expenses_taxation_plan'];

        $arr = ['work_cost','material_cost','machine_cost','safe_cost','labour_cost','else_cost','charges','expenses_taxation'];
        $list = ['办公','材料','机械','安全','劳务','其它','规费','税金'];
        
        //遍历查询数据
        for($i = 0 ; $i < 8 ; $i++){

            // var_dump($arr[$i],$arr_plan[$i]);
            // var_dump($project_name);
            // $cost = db('cost,dj_cost_plan ')
            //         ->field('dj_cost.'.$arr[$i],'dj_cost_plan.'.$arr_plan[$i])
            //         ->where(['dj_cost.'.$key=>'dj_cost_plan.'.$key, 'dj_cost.month' => 'dj_cost_plan.month'])
            //         ->select();
            // dj_cost.derate,dj_cost.rate_cutting
            $sql = 'select dj_cost.'.$arr[$i].',dj_cost_plan.'.$arr_plan[$i].',dj_cost.month from dj_cost,dj_cost_plan where dj_cost.'.$key.'=dj_cost_plan.'.$key." and dj_cost.month = dj_cost_plan.month and dj_cost.project_name='".$project_name['project_name']."'";
            $res[]= Db::query($sql);
        }
        $sql = "select count(month) from dj_cost where project_name = '".$project_name['project_name']."'";
        $count= Db::query($sql)[0]['count(month)'];
            // var_dump($res);die;
        // $cost= db('cost' as 'c','cost_plan' as 'cp')
        //         ->field('project_name.c,')
        //         ->where('project_name',$arr['project_name'])
        //         ->select();

            $m = 0;
            foreach ($res as  $v) {
                    // var_dump($v);die;
                foreach ($v as  $d) {
                    $data[$m] = $d;
                    @$data[$m]['derate'] = $d[$arr_plan[$m]] - $d[$arr[$m]];
                    @$data[$m]['rate_cutting'] = round($data[$m]['derate']/$d[$arr_plan[$m]],2);
                // var_dump($data);
                }
                    $m++;
            }
            $res[]= $data;
            $dd = $res[8];
            // var_dump($dd);die;
            $m = 0;
            // var_dump($res);
            // die;
        // 渲染页面
        return view('admin@cost/details',[
            'project_name'=>$project_name['project_name'],
            'list'=>$list,
            'res'=>$dd,
            'arr_plan'=>$arr_plan,
            'arr'=>$arr
        ]);

    }



}